/*
 * jQuery Input Limiter plugin 1.2.2
 * http://rustyjeans.com/jquery-plugins/input-limiter/
 *
 * Copyright (c) 2009 Russel Fones <russel@rustyjeans.com>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

(function ($) {
	$.fn.inputlimiter = function (options) {
		var opts = $.extend({}, $.fn.inputlimiter.defaults, options);
		
		$('#' + opts.boxId).html((opts.limit - $(this).html().length) + opts.partition + opts.limit);

		if (opts.boxAttach && !$('#' + opts.boxId).length) {
			$('<div/>').appendTo("body").attr({id: opts.boxId, 'class': opts.boxClass}).css({'position': 'absolute'}).hide();
			if ($.fn.bgiframe) {
				$('#' + opts.boxId).bgiframe();
			}
		}

		var filterHTML = function(str) {
			if(null!=str){
				if(opts.filterRuleA)
					return str.replace(/<.*?>/g, "").replace(new RegExp("&ensp;","g")," ").replace(new RegExp("&emsp;","g")," ").replace(new RegExp("&nbsp;","g")," ");
				else
					return str.replace(/<.*?>/g, "").replace(new RegExp("&ensp;","g")," ").replace(new RegExp("&emsp;","g")," ").replace(new RegExp("&nbsp;","g")," ");
			}else{
				return "";
			}
		};
		
		var filterHTMLAll = function(str) {
			if(null!=str){
				return str.replace(/<.*?>/g,"").replace(new RegExp("&ensp;","g")," ").replace(new RegExp("&emsp;","g")," ").replace(new RegExp("&nbsp;","g")," ");
			}else{
				return "";
			}
		};
		
		var inputlimiterKeyup = function (e) {
			var textLen = 0;
			if(opts.isdiveditor){
				textLen = filterHTMLAll($(this).html()).length;
			}else{
				$(this).val(filterHTML($(this).val()));
				textLen = filterHTMLAll($(this).val()).length;
			}
			
			if (!opts.allowExceed && textLen > opts.limit) {
				if(opts.isdiveditor){
					$(this).html(filterHTML($(this).html()).substring(0, opts.limit));
				}else{
					$(this).val(filterHTML($(this).val()).substring(0, opts.limit));
				}
			}
			
			if(opts.isdiveditor){
				textLen = filterHTMLAll($(this).html()).length;
			}else{
				textLen = filterHTMLAll($(this).val()).length;
			}
			
			if (opts.boxAttach) {				
			}
			
			var charsRemaining = textLen,
			    remText = opts.remTextFilter(opts, opts.limit - charsRemaining),
			    limitText = opts.limitTextFilter(opts);

			if (opts.limitTextShow) {
				$('#' + opts.boxId).html(remText + opts.partition + limitText);
				// Check to see if the text is wrapping in the box
				// If it is lets break it between the remaining test and the limit test
				var textWidth = $("<span/>").appendTo("body").attr({id: '19cc9195583bfae1fad88e19d443be7a', 'class': opts.boxClass}).html(remText + ' ' + limitText).innerWidth();
				$("#19cc9195583bfae1fad88e19d443be7a").remove();
				if (textWidth > $('#' + opts.boxId).innerWidth()) {
					$('#' + opts.boxId).html(remText + opts.partition + limitText);
				}
				// Show the limiter box
				$('#' + opts.boxId).show();
			} else {
				$('#' + opts.boxId).html(remText).show();
			}
		};

		var inputlimiterKeypress = function (e) {
			if (!opts.allowExceed && (!e.keyCode || (e.keyCode > 46 && e.keyCode < 90) || e.keyCode === 13) && $(this).html().length >= opts.limit) {
				return false;
			}
		};

		$(this).each(function (i) {
			$(this).bind('keyup', inputlimiterKeyup);
			$(this).bind('keypress', inputlimiterKeypress);
			//$(this).bind('blur', inputlimiterBlur);
		});
	};


	$.fn.inputlimiter.remtextfilter = function (opts, charsRemaining) {
		var remText = opts.remText;
		if (charsRemaining === 0 && opts.remFullText !== null) {
			remText = opts.remFullText;
		}
		remText = remText.replace(/\%n/g, charsRemaining);
		remText = remText.replace(/\%s/g, (opts.zeroPlural ? (charsRemaining === 1 ? '' : 's') : (charsRemaining <= 1 ? '' : 's')));
		return remText;
	};

	$.fn.inputlimiter.limittextfilter = function (opts) {
		var limitText = opts.limitText;
		limitText = limitText.replace(/\%n/g, opts.limit);
		limitText = limitText.replace(/\%s/g, (opts.limit <= 1 ? '' : 's'));
		return limitText;
	};

	$.fn.inputlimiter.defaults = {
		limit: 255,
		boxAttach: true,
		partition: '/',
		boxId: '',
		boxClass: '',
		remText: '%n',
		remTextFilter: $.fn.inputlimiter.remtextfilter,
		remTextHideOnBlur: true,
		remFullText: null,
		limitTextShow: true,
		limitText: '%n',
		filterRuleA: false,
		limitTextFilter: $.fn.inputlimiter.limittextfilter,
		zeroPlural: true,
		allowExceed: false,
		isdiveditor: false	//判断是否是DIV编辑框
	};

})(jQuery);
